package worker;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import bookshopSM.BookshopGUI;

public class AddWorker extends JPanel{
    private JButton clearButton, writeButton;
    private BookshopGUI userInterface;
    String names1[] = { "员工号","姓      名","性      别","年      龄","初始密码","联系方式" ,"家庭住址","备      注"};
    static  Connection conn = null;
    private static String username ="root";
    private static String password="root" ;
    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3307/books_system";

    public AddWorker(){

        JLabel ltitle = new JLabel("添加新员工        ");
        ltitle.setFont(new   java.awt.Font("楷体", 1, 20));
        Box box1 = Box.createVerticalBox();
        userInterface = new BookshopGUI( names1 );
        box1.add(Box.createVerticalStrut(15));
        box1.add(ltitle );
        box1.add(Box.createVerticalStrut(15));
        box1.add(userInterface);
        this.add( box1);
        writeButton =userInterface.getDoTask1Button();
        writeButton.setText( "确认" );
        writeButton.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        String fieldValues[] = userInterface.getFieldValues();
                        try {
                            conn = DriverManager.getConnection(url,username,password);
                            Statement stmt=conn.createStatement();
                            stmt.executeUpdate("INSERT INTO Worker(Wname,Wsex,Wage,Wphone,Wadd,Wnote) " +
                                    "VALUES ('"+fieldValues[1]+"','"+
                                    fieldValues[2]+"','"+
                                    fieldValues[3]+"','"+
                                    fieldValues[5]+"','"+
                                    fieldValues[6]+"','"+
                                    fieldValues[7]+
                                    "')");
                            ResultSet rs = stmt.executeQuery("select top 1 Wno from Worker order by Wno desc");
                            rs.next();//rs指向第一行之前
                            int newvno=rs.getInt("Wno");
                            userInterface.fields[0].setText(String.valueOf(newvno));
                            userInterface.fields[4].setText("123456");
                            JOptionPane.showMessageDialog(null, "添加成功");
                        }
                        catch(SQLException e1){
                            System.out.println("请正确输入");
                        }
                        catch (Exception ee){
                            System.out.println(ee);
                        }
                        finally{
                            if(conn!=null){
                                try {
                                    conn.close();
                                } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                conn=null;
                            }
                        }
                    }
                }


        );
        clearButton = userInterface.getDoTask2Button();
        clearButton.setText( "清除" );
        clearButton.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        userInterface.clearFields();
                    }
                }
        );
    }
}

